import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login'
import Layout from '@/views/layout'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: () => import('@/views/home')
      },
      {
        path: '/art-cate', // 文章分类
        component: () => import('@/views/article')
      },
      {
        path: '/art-list', // 文章列表
        component: () => import('@/views/list')
      },
      {
        path: '/publish', // 发布文章
        component: () => import('@/views/article/publish')
      },
      {
        path: '/user-avatar', // 更换头像
        component: () => import('@/views/user/header')
      },
      {
        path: '/user-info', // 基本资料
        component: () => import('@/views/user/information')
      },
      {
        path: '/user-pwd', // 更改密码
        component: () => import('@/views/user/password')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 前值守卫 登录注册页面可以访问，其他页面拦截
router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  if (to.path !== '/login' && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router
